
from enum import Enum
from queue import Queue
from datetime import datetime, timedelta


class EventType(Enum):
    SIG_FIRST_INIT = 0
    ETHERPAD_DOWNLOAD_ERR = 100
    ETHERPAD_NOTHING_CHANGED = 2
    ETHERPAD_CHANGED = 3
    MEETING_NEW = 200
    MEETING_ALARM = 201
    MEETING_NOTHING_CHANGED = 202


class Event(object):
    def __init__(self, event_type, sig, event_time, content):
        self.event_type = event_type
        self.sig = sig
        self.event_time = event_time
        self.content = content


class EventQueue(object):
    event_queue = Queue()

    def __init__(self):
        assert False

    @staticmethod
    def put(event):
        EventQueue.event_queue.put(event)

    @staticmethod
    def get():
        return EventQueue.event_queue.get()

    @staticmethod
    def getall():
        events = []
        while not EventQueue.event_queue.empty():
            events.append(EventQueue.event_queue.get())
        return events

    @staticmethod
    def empty():
        return EventQueue.event_queue.empty()


class TimeKV(object):

    def __init__(self, time_str, val):
        self.time_str = time_str
        self.val = val
        self.time_val = datetime.strptime(
            time_str if time_str is not None and time_str != '' else '1997-01-11 0:00',
            '%Y-%m-%d %H:%M')


class NetSpider(object):
    inner = None

    @staticmethod
    def init(inner):
        NetSpider.inner = inner

    @staticmethod
    def get_text(url):
        return NetSpider.inner.get_text(url)

    @staticmethod
    def get_json(url):
        return NetSpider.inner.get_json(url)


class Messager(object):
    inner = None

    @staticmethod
    def init(inner):
        Messager.inner = inner

    @staticmethod
    def send(msg, recv, recv2=''):
        Messager.inner.send(msg, recv)

        if recv2 is None or recv2 == '':
            return
        Messager.inner.send(msg, recv2)

    @staticmethod
    def get_json(url):
        return NetSpider.inner.get_json(url)